App({
  userInfo: {avatar:'https://minip.oss-cn-beijing.aliyuncs.com/WX20180320-182106.png?Expires=1521544973&OSSAccessKeyId=TMP.AQHjE-QeNztiPhmESuMS16TSbgIZA6encSCz_xjR6t5KPS87g8exZW4nCCNbADAtAhQwgiYY_D35KBTFmQuQ6A8f0pXJ8AIVAPuux_F1nagMfIrzf3sGDBsjFzqp&Signature=nFyL2JFRYDvKKdRDCCC30JRE4T8%3D',nickName:''}, //用户信息
  APP_ID:"2018012702090304",
  auth_code:'',
  // access_token:'',


  //免登获取用户信息
   getUserInfo() {
    console.log('in get user info');

    return new Promise((resolve, reject) => {
      // if (this.userInfo) resolve(this.userInfo);
    // console.log('in get user info2');
 setTimeout(function () {
      my.getAuthCode({
        scopes: ['auth_base'],
        success: (authcode) => {
          console.log(authcode);
          // resolve({data:this.auth_code});
          my.getAuthUserInfo({
            success: (res) => {
              console.log(res);
              // debugger;
              console.log('11');
              var userInfo = {avatar:'',nickName:''};
              console.log('22');
              
              userInfo.avatar = res.avatar;
              userInfo.nickName = res.nickName;
              userInfo.auth_code = authcode.authCode;
               
              resolve({data:userInfo});
              // this.userInfo.avatar = userInfo.avatar;

            },
            fail: () => {
              var user_info = {avatar : 'https://minip.oss-cn-beijing.aliyuncs.com/WX20180320-182106.png?Expires=1521544973&OSSAccessKeyId=TMP.AQHjE-QeNztiPhmESuMS16TSbgIZA6encSCz_xjR6t5KPS87g8exZW4nCCNbADAtAhQwgiYY_D35KBTFmQuQ6A8f0pXJ8AIVAPuux_F1nagMfIrzf3sGDBsjFzqp&Signature=nFyL2JFRYDvKKdRDCCC30JRE4T8%3D' };
                    this.data.userInfo.avatar = 'https://minip.oss-cn-beijing.aliyuncs.com/WX20180320-182106.png?Expires=1521544973&OSSAccessKeyId=TMP.AQHjE-QeNztiPhmESuMS16TSbgIZA6encSCz_xjR6t5KPS87g8exZW4nCCNbADAtAhQwgiYY_D35KBTFmQuQ6A8f0pXJ8AIVAPuux_F1nagMfIrzf3sGDBsjFzqp&Signature=nFyL2JFRYDvKKdRDCCC30JRE4T8%3D';
              this.setData({
                    userInfo : user_info
              });
              resolve(-1);
              reject({});
            },
          });
        },
        fail: () => {
              console.log('in fail');    
              resolve(-1);      
          reject({
            
          });
        },
      });
    });
     },0);
  },

  //调用服务器端接口,服务器端获取支付宝token,login
   login(auth){
    console.log(auth);
      var appid = this.APP_ID
      var url ='http://zfapi.dorabox.com.cn/api/login'
      // var url ='http://fengbobdemacbook-air.local/paypro/web/index.php/api/login'
      var nN = this.globadData.userInfo.nickName;
      var av = this.globadData.userInfo.avatar;
    return new Promise((resolve, reject) => {
      setTimeout(function () {
      // if (this.access_token ) resolve(this.access_token);


      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
          "auth_code":auth,
          "nickName":nN,
          "avatar":av,
          "app_id":appid
        }),
        dataType:'json',
        success: (res) => {
          console.log(res);
        if(res.data.status==0)
        {
          console.log('ok');
          console.log(res.data.data);
          // this.globadData = {};
          // this.globadData.access_token = res.data.data;
          resolve(res.data.data);
        }
      },
    });
    }, 0);
    });
    
  },

  //调用服务器端接口,服务器端获取游客token,login
   touristLogin(auth){
    console.log(auth);
      var appid = this.APP_ID
      var url ='http://zfapi.dorabox.com.cn/api/tourist-login'
      // var url ='http://fengbobdemacbook-air.local/paypro/web/index.php/api/login'
      
    return new Promise((resolve, reject) => {
      setTimeout(function () {
      // if (this.access_token ) resolve(this.access_token);


      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
          "app_id":appid
        }),
        dataType:'json',
        success: (res) => {
          console.log(res);
        if(res.data.status==0)
        {
          console.log('ok');
          console.log(res.data.data);
          // this.globadData = {};
          // this.globadData.access_token = res.data.data;
          resolve(res.data.data);
        }
      },
    });
    }, 0);
    });
    
  },


  //提交订单
  submit_order(){

              // var app = getApp();
              var url = this.globadData.apiUrlHeader+'add-order'
            // var id = this.data.goods_list.goods_id
            // var jj = {
            //           "goods":this.globadData.goods_list,
            //           "address_id":this.globadData.order_address_id,
            //           "access_token":this.globadData.access_token
            //         };
                    console.log(this.globadData.order_remark);
                    if(this.globadData.order_remark == '' || this.globadData.order_remark == undefined){
                      this.globadData.order_remark = ' ';
                    }
            return new Promise((resolve, reject) => {
               my.httpRequest({
                   url: url, // 目标服务器 url
                   method:'Post',
                   headers:{'Content-Type':'Application/json'},
                   data:JSON.stringify({
                      "goods":this.globadData.goods_list,
                      "address_id":this.globadData.order_address_id,
                      "remark":this.globadData.order_remark,
                      "access_token":this.globadData.access_token
                    }),
                  dataType:'json',
                  success: (res) => {
                     console.log(res.data);
                    //  for(var i=0;i<res.data.data.length;i++){
                    //    res.data.data.quantity = parseInt(res.data.data.quantity);
                    //  }
                    //  this.setData({
                    //    items:res.data.data
                    //  });
                    resolve(true);
                   },
               });
            },0);
            

            },



  getPayOrderStr(order_id){
      var appid = this.APP_ID
      var url = this.globadData.apiUrlHeader+"pay";
      var token = this.globadData.access_token;
    return new Promise((resolve, reject) => {
      setTimeout(function () {
      // if (this.access_token ) resolve(this.access_token);


      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
          "order_id":order_id,
          "access_token":token
        }),
        dataType:'json',
        success: (res) => {
          console.log(res);
        if(res.data.status==0)
        {
          console.log('ok');
          console.log(res.data.data);
          // this.globadData = {};
          // this.globadData.access_token = res.data.data;
          resolve(res.data.data);
        }
      },
    });
    }, 0);
    });
    
  },



  onLaunch(options) {
   
  },
  onShow(options) {
    //小程序显示
    // console.log(options);

  },
  onHide() {
    //小程序隐藏
  },
  onError(msg) {
    //小程序发生错误
    console.log(msg);
  },

  //全局变量
  globadData: {
    //购物车
    // {"good_id":"1","good_img":"/images/shouye_04.png","good_name":"产品1",
      // "good_old_price":"108","good_selling_price":"98","quantity":24,"good_is_checked":false},
    carts:[],
    isTourist : false,
    order_address_id:'',
    order_remark:'',
    access_token:'',
    writeBy: 'jiangge',
    userInfo: {avatar:'',nickName:''}, //用户信息 
    // apiUrlHeader: 'http://fengbobdemacbook-air.local/paypro/web/index.php/api-zfb/',
      // apiUrlHeader: 'http://localhost/paypro/index.php/api/'
    // apiUrlHeader: 'http://zfapi.dorabox.com.cn/api-zfb/'
    apiUrlHeader: 'http://test.paypro/index.php/api-zfb/'
    // apiUrlHeader: 'http://fengbobdemacbook-air.local/paypro/web/index.php/api/'
  },

  requestJson(url, data, successFun, failFun) {
    my.httpRequest({
      url: this.globadData.apiUrlHeader + url, // 目标服务器 url
      method: 'Post',
      headers: { 'Content-Type': 'Application/json' },
      data: JSON.stringify(data),

      success: function (res) {
        successFun(res);
      },
      failFun: function (res) {
        failFun(res);
      },
      complete: function (res) {
       
      }
    });
  },
});
